package com.lpinfo.spb.system;

import com.alibaba.fastjson.JSON;
import com.lpinfo.spb.LpinfoSpbApplicationTests;
import com.lpinfo.spb.plateform.system.controller.SysUserController;
import com.lpinfo.spb.plateform.system.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@WebAppConfiguration
@Transactional
@Rollback(true)
@Component
public class LpinfoSpbControllerSuit extends LpinfoSpbApplicationTests {

    @Autowired
    private SysUserController sysUserController;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        mockMvc = MockMvcBuilders.standaloneSetup(sysUserController).build();
    }

    @Test
    public void page() throws Exception {
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get("/user/page")
                //请求的报文MiNi类型
                .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                //接受的报文类型
                .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
                .param("current", "1")
                .param("size", "10");
        MvcResult mvcResult = mockMvc.perform(mockHttpServletRequestBuilder)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }

    @Test
    public void getById() throws Exception {
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get("/user/getById/1")
                //请求的报文MiNi类型
                .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                //接受的报文类型
                .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
                .param("username", "admin");
        MvcResult mvcResult = mockMvc.perform(mockHttpServletRequestBuilder)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }

    @Test
    public void save() throws Exception {
        Long primaryKey=System.currentTimeMillis();
        SysUser insertEntity  = new SysUser();
        insertEntity.setUserId(primaryKey);
        insertEntity.setUserName("zhangsan");
        insertEntity.setUserPassword("123456");
        insertEntity.setNickName("张三");
        insertEntity.setCreateTime(LocalDateTime.now());
        String jsonString = JSON.toJSONString(insertEntity);

        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.put(  "/user/save")
                .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                .content(jsonString)
                .accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
        MvcResult mvcResult = mockMvc.perform(mockHttpServletRequestBuilder)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }


}
